﻿#region 头注释
/******************************************************************************
文件名称    :   PatientService.cs
创建日期    :   2011-4-23 18:34:05
功能描述    :   病人信息实体数据服务类
修改日志    :   
    2011-04-23 18:34  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Stone.Components.Entities;
using Dare.Utilities.Data;
using Stone.Data.Providers;

namespace Stone.Services.EntityServices
{
    /// <summary>
    /// 病人信息实体数据服务类
    /// </summary>
    public class PatientService : EntityServiceTemplate<Patient, long, PatientDataProvider>
    {
        public PatientService(Application app)
            : base(app)
        {
        }


        public List<Patient> SearchPatientInfo(string querySql, string patientName, string orderBy, int pageIndex, int pageSize, out int totalCount)
        {
            List<Patient> list = null;
            int itemCount = 0;
            if (Execute(delegate(TransactionManager manager)
            {
                list = provider.SearchPatientInfo(manager, querySql, patientName, orderBy, pageIndex, pageSize, out itemCount);
            }))
            {
                totalCount = itemCount;
                return list;
            }
            else
            {
                totalCount = -1;
                return null;
            }
        }


        public bool DeletePatient(Patient patient)
        {
            HistoryService historyService = Stone.Services.Application.Instance.GetDbService<HistoryService>();
            HospitalizationService hospitalizationService = Stone.Services.Application.Instance.GetDbService<HospitalizationService>();

            try
            {
                if (patient.HistoryId.HasValue)
                {
                    historyService.DeleteHistoryInfo(historyService.Get(patient.HistoryId.Value));
                }
                
                List<Hospitalization> hospitals = hospitalizationService.GetAllByPatient(patient.Id, null);
                foreach (Hospitalization hospital in hospitals)
                {
                    hospitalizationService.DeleteHospitalInfo(hospital);
                }

                return Delete(patient.Id);
            }
            catch
            {
                return false;
            }
        }
    }
}
